package org.learn.mq.rabbit.exchange;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

/**
 * <一句话描述>
 *
 * @author xianglujun
 * @date 2019/7/8 14:52
 */
@Slf4j
public class Publisher {

    private static final String QUEUE_NAME = "logs";
    private static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.44.128");
        try {
            // 创建一个rabbitmq链接对象
            Connection connection = factory.newConnection();
            // 创建Channel对象, 该channel对象, 不需要我们显式的执行关闭操作
            Channel channel = connection.createChannel();

            // 创建一个广播的exchange
            channel.exchangeDeclare(EXCHANGE_NAME, "fanout");

            // 通过命令行的方式传输数据
            String message = "This is a publisher message";

            channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, null, message.getBytes());

            System.out.println("[x] Sent '" + message + "'");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
